package com.mall.manager.babytop.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.util.WebUtils;

import com.mall.common.attributes.EnumType;
import com.mall.untils.Page;
/**
 * 宝宝单题信息
 * @author tgy
 *
 */
@Service
public class BabyTopService {
	
	private Logger logger = LoggerFactory.getLogger(BabyTopService.class);
	
	 @Resource(name="jdbcTemplate")
	 private JdbcTemplate jdbcTemplate;
	
	 /**
	  * 分页查询宝宝单题信息
	  * @param page
	  * @param request
	  * @return
	  */
	@SuppressWarnings("rawtypes")
	public Page findBabyTop(Page page,HttpServletRequest request){
		
	    Map paraMap = WebUtils.getParametersStartingWith(request, "");
	    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
	    //数据类型：SDK,HTML,PC
	    String data_type = MapUtils.getString(paraMap, "dataType");
	    int pageSize = 10;
	    try{
	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
	        StringBuffer sql = new StringBuffer("select count(*) from db_processing.t_single_topic as s where 1=1");    
	        StringBuffer sbf = new StringBuffer("select * from db_processing.t_single_topic as s where 1=1");
			//数据类型查询SDK,HTML,PC
			if(StringUtils.isNotEmpty(data_type)){
				sbf.append(" and s.data_type=").append(EnumType.DATA_TYPE_STATUS.getType(data_type));
				sql.append(" and s.data_type=").append(EnumType.DATA_TYPE_STATUS.getType(data_type));
			}
	        //总记录数
	        Integer totalSize = this.jdbcTemplate.queryForObject(sql.toString(), Integer.class); 
	        //开始页
	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
			sbf.append(" limit ").append(startIndex).append(",").append(pageSize);
	        List list = this.jdbcTemplate.queryForList(sbf.toString());
	        page = new Page(startIndex, totalSize, pageSize, list);
	    }catch(Exception e){
	    	logger.info("分页查询宝宝单题信息出现异常："+e.getMessage());
	    }
		return page;
	}
}
